#!/usr/bin/python3
import sys
import math
import re

def part1():
    print('part 1')
    total = 0
    for line in sys.stdin:
        left, right = line.strip().split('|', 1)
        winners = sorted([int(num) for num in re.split(r'\s+', left.split(':')[1].strip())])
        picked = sorted([int(num) for num in re.split(r'\s+', right.strip())])
        points, w, p = 1, 0, 0
        while w < len(winners) and p < len(picked):
            winner = winners[w]
            pick = picked[p]
            if winner == pick:
                points *= 2
            if winner <= pick:
                w += 1
            if winner >= pick:
                p += 1
        points //= 2
        print(points, winners, picked)
        total += points
    print(total)

def part2():
    print('part 2')
    copies = {}
    card = 1
    for line in sys.stdin:
        left, right = line.strip().split('|', 1)
        winners = sorted([int(num) for num in re.split(r'\s+', left.split(':')[1].strip())])
        picked = sorted([int(num) for num in re.split(r'\s+', right.strip())])
        w, p = 0, 0
        next_copy = card + 1
        copies[card] = copies.get(card, 0) + 1
        while w < len(winners) and p < len(picked):
            winner = winners[w]
            pick = picked[p]
            if winner == pick:
                copies[next_copy] = copies.get(next_copy, 0) + copies[card]
                next_copy += 1
            if winner <= pick:
                w += 1
            if winner >= pick:
                p += 1
        print(winners, picked)
        card += 1
    print(copies)

    summed = 0
    for key in copies:
        summed += copies[key]
    print(summed)

if sys.argv[1] in '1':
    part1()
else:
    part2()
